#include <queue>
#include <iostream>
using namespace std;
int dx[4] = { 0, 0, 1, -1 };
int dy[4] = { 1, -1, 0, 0 };

int countPath(vector<vector<int> >& CityMap, int n, int m) {
    queue<pair<int, int>>q;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (CityMap[i][j] == 1) {
                q.push({ i, j });
            }
        }
    }

    int ret = 0;
    while (q.size()) {
        if (ret)
            break;
        int sz = q.size();
        while (sz--) {
            auto p = q.front();
            int a = p.first, b = p.second;
            q.pop();
            CityMap[a][b] = -1;
            for (int i = 0; i < 4; i++) {
                int x = a + dx[i], y = b + dy[i];
                if (x >= 0 && x < n && y >= 0 && y < m && CityMap[x][y] != -1 && CityMap[x][y] != 2) {
                    q.push({ x, y });
                }
                else if (x >= 0 && x < n && y >= 0 && y < m && CityMap[x][y] == 2) {
                    ret++;
                }
            }
        }
    }

    return ret;
}

int main()
{
    vector<vector<int> > CityMap = { {0,1,0},{2,0,0} };
    cout << countPath(CityMap, 2, 3) << endl;
    return 0;
}